﻿using System;
using System.Collections.Generic;
using System.Windows.Data;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using carXapp2;
using System.Collections.ObjectModel;
using System.ComponentModel;
using carXapp2.classes;

using System.Threading;

namespace carXapp2.FuncPages
{
    public partial class RemiderInfo : PhoneApplicationPage
    {
        private int CarID;

        public RemiderInfo()
        {
            InitializeComponent();
        }

        private ObservableCollection<reminderInfo> _Rinfo;

        public ObservableCollection<reminderInfo> CurrentRInfo
        {
            get { return _Rinfo; }
            set
            {
                _Rinfo = value;
                NotifyPropertyChanged("CurrentRInfo");
            }

        }
        private void LoadData(int id)
        {
            var recInDB = from reminderInfo data in App.ViewModel.carDataDb.reminderInfo
                          where data.CarID == id
                          select data;
            App.ViewModel.Reminder = new ObservableCollection<reminderInfo>(recInDB);

            this.DataContext = App.ViewModel;

        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            string cid;
            NavigationContext.QueryString.TryGetValue("carID", out cid);
            int.TryParse(cid,out CarID);
            LoadData(CarID);
            base.OnNavigatedTo(e);
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        private void menuADD_Click(object sender, EventArgs e)
        {
            reminderInfo rInfo = new reminderInfo();
            rInfo.Date = DateTime.Now;
            App.ViewModel.SingleReminder = rInfo;
            NavigationService.Navigate(new Uri("/MiscPages/AddEditReminder.xaml?CarID="+CarID, UriKind.RelativeOrAbsolute));
     
        }

        private void menuEdit_Click(object sender, EventArgs e)
        {
            if (listbox.SelectedIndex != -1)
            {
                var selected = listbox.SelectedItem as reminderInfo;
                App.ViewModel.SingleReminder = selected;
                int rid = App.ViewModel.SingleReminder.ReminderID;
                NavigationService.Navigate(new Uri("/MiscPages/AddEditReminder.xaml?ReminderID=" + rid, UriKind.RelativeOrAbsolute));
            }
            else
            {
                MessageBox.Show("please select a record");
            }
        }

        private void menuDel_Click(object sender, EventArgs e)
        {
            if (listbox.SelectedIndex != -1)
            {
                var selRecord = listbox.SelectedItem as reminderInfo;
                App.ViewModel.DeleteReminder(selRecord);
            }
            else
            {
                MessageBox.Show("please select a record");
            }
        }
        
    }
}